package day_2022_12_to_3.year2023;

import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

/**
 * @author haomin
 * @date 2023/03/19 18:57
 **/
public class Day3_19_Num1625 {
    class Solution {
        public String findLexSmallestString(String s, int a, int b) {
            char[] cc = s.toCharArray();
            Set<String> set = new HashSet<>();
            dfs(set, cc, a, b);
            PriorityQueue<String> priority = new PriorityQueue<>();
            for (String ss : set) {
                priority.offer(ss);
            }
            return priority.peek();
        }

        private void dfs(Set<String> set, char[] cc, int a, int b) {
            if (!set.add(new String(cc))) {
                return;
            }
            char[] c1 = new char[cc.length];
            char[] c2 = new char[cc.length];
            handle1(c1, cc, a);
            handle2(c2, cc, b);
            dfs(set, c1, a, b);
            dfs(set, c2, a, b);
        }

        private void handle1(char[] tar, char[] cc, int a) {
            for (int i = 0; i < cc.length; i += 2) {
                tar[i] = cc[i];
            }
            int var;
            for (int i = 1; i < cc.length; i += 2) {
                var = cc[i] - '0';
                var += a;
                var %= 10;
                tar[i] = (char) var;
                tar[i] += '0';
            }
        }

        private void handle2(char[] tar, char[] cc, int b) {
            int len = cc.length;
            for (int i = 0; i < cc.length; i++) {
                tar[(i + b) % len] = cc[i];
            }
        }
    }
}